// 引用百度地图微信小程序JSAPI模块 
var bmap = require('../../utils/bmap-wx.js')
var wxMarkerData = []
Page({
  data: {
    cid: 0,
    movesrc: "left_move"
  },  
  makertap: function (e) {
    var that = this
    var id = e.markerId
    that.showSearchInfo(wxMarkerData, id)
    that.changeMarkerColor(wxMarkerData, id)
  },
  makerlefttap: function (e) {
    var that = this
    var currentid = this.data.cid
    if (currentid <= 0) {
      return
    }
    else{
      if (currentid == 1) {
        that.showSearchInfo(wxMarkerData, currentid - 1)
        that.changeMarkerColor(wxMarkerData, currentid - 1)
        that.setData({
          cid: currentid - 1,
          movesrc: "left_move"
        })
      }
      else{
        that.showSearchInfo(wxMarkerData, currentid - 1)
        that.changeMarkerColor(wxMarkerData, currentid - 1)
        that.setData({
          cid: currentid - 1,
          movesrc: "middle_move"
        })
      }      
    }    
  },
  makerrighttap: function (e) {
    var that = this
    var currentid = this.data.cid    
    var idnum = this.data.markernum
    if (currentid >= idnum-1) {
      return
    }
    else {
      if (currentid == idnum - 2){
        that.showSearchInfo(wxMarkerData, currentid + 1)
        that.changeMarkerColor(wxMarkerData, currentid + 1)
        that.setData({
          cid: currentid + 1,
          movesrc: "right_move"
        })
      }
      else{
        that.showSearchInfo(wxMarkerData, currentid + 1)
        that.changeMarkerColor(wxMarkerData, currentid + 1)
        that.setData({
          cid: currentid + 1,
          movesrc: "middle_move"
        })
      }
    }    
  },
  onLoad: function (options) {    
    this.setData({
      title: options.title
    })
    /* 用户是否授权 获取经纬度 */
    wx.getLocation({
      type: 'wgs84', //默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标   
      success: function (res) { //如果用户授权，点击确定，获取所在城市以及经纬度        
        var longitude = res.longitude
        var latitude = res.latitude
        that.setData({
          long: res.longitude,
          lat: res.latitude
        })
      },
      fail: function (res) { //如果用户拒绝授权，点击取消，全部值置为空
        console.log('拒绝授权')
      }
    })   
    var that = this
    // 新建百度地图对象 
    var BMap = new bmap.BMapWX({
      ak: 'ZenlWcczwXIAsrvdnGvFkdCSTRxWSXep'
    })
    var fail = function (data) {
      console.log("fail",data)
    }
    var success = function (data) {
      wxMarkerData = data.wxMarkerData;
      if (data.wxMarkerData.length == 0) {  //周边设施为空，显示当前位置，避免报错
        that.setData({
          latitude: that.data.lat,
          longitude: that.data.long,
          markernum: wxMarkerData.length,
          address: "无"
        })
      }
      else {
        that.setData({
          markers: wxMarkerData,
          latitude: wxMarkerData[0].latitude,
          longitude: wxMarkerData[0].longitude,
          markernum: wxMarkerData.length
        })
        that.showSearchInfo(that.data.markers, 0);  //默认显示第一个id内容
        that.changeMarkerColor(wxMarkerData, 0);    //默认显示第一个id图标
      }
    }
    // 发起POI检索请求 
    BMap.search({
      "query": that.data.title,
      fail: fail,
      success: success, 
      iconPath: "/images/marker_blue.png"
    })  
    wx.getSystemInfo({
      success: function (res) {
        let height = res.windowHeight              
        wx.createSelectorQuery().select('.title').boundingClientRect(function (rect) {
          that.setData({
            //scroll-view高度为屏幕高度减去上一个节点（'.title'）底部的纵坐标，单位为px
            scrollHeight: res.windowHeight - rect.bottom            
          });          
        }).exec()
      }
    })
  },
  showSearchInfo: function (data, i) {
    var that = this;
    that.setData({
      placeData: {
        title: data[i].title,        
        telephone: data[i].telephone         
      },
      address: data[i].address.split(";"),
    });
  },  
  changeMarkerColor: function (data, i) {
    var that = this  
    var markers = []
    for (var j = 0; j < data.length; j++) {
      if (j == i) {
        // 此处需要在相应路径放置图片文件 
        data[j].iconPath = '/images/marker_blue_active.png'   
      } else {
        // 此处需要在相应路径放置图片文件 
        data[j].iconPath = "/images/marker_blue.png"
      }
      markers[j] = data[j]
    }
    that.setData({
      markers: markers
    });
  },
  //滑动开始事件
  handletouchtart: function (e) {
    this.data.lastX = e.touches[0].pageX
    this.data.lastY = e.touches[0].pageY
  },
  //滑动结束事件
  handletouchend: function (event) {
    if (this.data.currentGesture < 0) {  //向右边id-1      
      this.makerlefttap()
    } 
    else if (this.data.currentGesture > 0) {  //向左边id+1      
      this.makerrighttap()
    }
    else return
    this.data.currentGesture = 0
  },
  //滑动移动事件
  handletouchmove: function (event) {
    var currentX = event.touches[0].pageX
    var currentY = event.touches[0].pageY
    var tx = currentX - this.data.lastX
    var ty = currentY - this.data.lastY    
    //左右方向滑动
    if (Math.abs(tx) > Math.abs(ty)) {
      if (Math.abs(tx) >= 20){
        if (tx < 0)
          this.data.currentGesture = 1
        else if (tx > 0)
          this.data.currentGesture = -1
      }
    }
  }
})